requirejs.config({
    baseUrl: '../js/libs',
    paths: {
        jquery: 'jquery.min',
        jquery_route: 'jquery.route',
        bootstrap: "../../bootstrap-3.3.5/dist/js/bootstrap.min",
        bootstrap_paginator:"../plugins/bootstrap-paginator/bootstrap-paginator",
        config: "../common/config",
        pageJs: '../common/pageJs',
        common:'../common/common',
        arttemplateUtil: '../js/common/arttemplate-util',
        validform:"../plugins/Validform/Validform_v5.3.2"
    },
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'jquery': {
            exports: '$'
        },
        'bootstrap':['jquery'],
        'bootstrap_paginator':{
        	deps:['bootstrap'],
        	exports: 'bootstrp'
        },
        //'bootstrap_paginator':['bootstrap'],
        //'pageJs':['bootstrap_paginator'],
        'validform':['jquery']
    }
});
requirejs(['jquery', 'pageJs','config', 'arttemplateUtil','common','validform'], 
		function($, pageJs,config, arttemplateUtil,common) {
	var param = {};
	var initData = function(){
		common.loading();
		common.common_ajax_request('/api/v1/vip_trans_area_list',param,false,function(data,code,msg){
			//console.info(JSON.stringify(data));
			setData(data);
			pageJs.setPage(data.pagenum,data.totalPage,data.totalCount,callback);
			initBtn();
			layer.closeAll('loading'); //关闭加载层
		},"","post");
	};
	var arealist_tpl;//
	var initTpl = function(){
		arealist_tpl = arttemplateUtil.template.compile($.setHTML('logistics/tpl/distribution_area_tpl.html'));
	};
	//
	var setData = function(data){
		var tempList = data.dataList;
		for(var i=0; i<tempList.length; i++){
			var temp=tempList[i];
			var info = JSON.parse(temp.tran_area_info);
			if(info.pid_names){
				var pnameArr = info.pid_names.split(",");
				var ppMap = {};
				for(var j=0; j<pnameArr.length; j++){
					var aa = pnameArr[j].split("@");
					if(ppMap[aa[0]] == undefined){  
					  	var list = [];  
					    list.push(aa[1]);  
					    ppMap[aa[0]]=list;
					}else{  
					    ppMap[aa[0]].push(aa[1]);  
					}
				}
				var pIdArr = info.parent_ids.split(",");
				var pNameArr = info.parent_names.split(",");
				var mArr = [];
				for(var pp in ppMap){
					var pidIndex = pIdArr.indexOf(pp);
					mArr.push(pNameArr[pidIndex]?(pNameArr[pidIndex]+":"+ppMap[pp]):ppMap[pp]);
				}
				temp.community_names = mArr;
			}else{
				temp.community_names = [info.community_names];
			}
		}
		var res = {
			records:tempList
		};
		$("#arealist").html(arealist_tpl(res));
	};
	
	var callback = function(page){
		param.pageNo=page;
		initData();
	};
	
	var initTab = function(){
		var tab = $("#tab_id").val();
		if(tab == 2){
			$("#tab_tit1").removeClass("active");
			$("#tab_tit2").addClass("active");
			$("#panel-1").removeClass("active");
			$("#panel-2").addClass("active");
		}
	};
	var initBtn = function(){
		$(".st-edit-btn").click(function(){
			var id = $(this).attr("data-id");
			window.location.href="#logistics/addset/"+id;
		});
		$(".st-del-btn").click(function(){
			var id = $(this).attr("data-id");
			var name=$(this).attr("trans_name");
			layer.confirm('确定删除吗？', {
				  btn: ['确定','取消']
			}, function(){
				common.loading();
				var param = {id: id, trans_name:name};
				common.common_ajax_request('/api/v1/vip_trans_area_del',param,false,function(data,code,msg){
					if(code==200 && msg.businessCode==0){
						initData();
						layer.alert("删除成功!");
					}else{
						layer.alert("删除失败");
					}
				},"","post");
			}, function(){});
		});
	};
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	var setPage2 = function(curPage,allPage,allNum,callback) {
		if(curPage==0 || allPage==0){//不显示页数
			$("#pagebox2").hide();
		}else{
			var options = {
					currentPage:curPage,//当前页
					totalPages:allPage,//总页数
					numberOfPages:5,//显示页数
					bootstrapMajorVersion:3,
					itemTexts: function(type, page, current){
						switch (type) {
		                case "first":
		                    return "首页";
		                case "prev":
		                    return "上一页";
		                case "next":
		                    return "下一页";
		                case "last":
		                    return "末页";
		                case "page":
		                    return page;
		                }
					},onPageClicked:function (event, originalEvent, type, page){
						callback(page,type,originalEvent,event);
					}
				}
			$("#records2").html(allNum);
			$("#page2").bootstrapPaginator(options);
			$("#pageBox2").show();
		}
	};
	var param2 = {};
	var initData2 = function(){
		common.loading();
		common.common_ajax_request('/api/v1/store_frequency_list',param2,false,function(data,code,msg){
			//console.info(JSON.stringify(data));
			setData2(data);
			setPage2(data.pagenum,data.totalPage,data.totalCount,callback2);
			initBtn2();
			layer.closeAll('loading'); //关闭加载层
		},"","post");
	};
	var arealist_tpl2;//
	var initTpl2 = function(){
		arealist_tpl2 = arttemplateUtil.template.compile($.setHTML('logistics/tpl/store_frequency_tpl.html'));
	};
	//
	var setData2 = function(data){
		var tempList = data.dataList;
		var tList = [];
		for(var i=0; i<tempList.length; i++){
			var obj={};
			var temp=tempList[i];
			obj.id=temp.id;
			obj.temp_name = temp.temp_name;
			var list = temp.storeFrequencyList;
			var tContent = [];
			for(var j=0; j<list.length; j++){
				tContent.push({frequency:list[j].frequency, name:list[j].name.replace(new RegExp(/(,)/g),"/")});
			}
			obj.temp_content = tContent;//.join("\r\n");
			tList.push(obj);
		}
		var res = {
			records:tList
		};
		$("#store_frequency_list").html(arealist_tpl2(res));
	};
	
	var callback2 = function(page){
		param2.pageNo=page;
		initData2();
	};
	var initBtn2 = function(){
		$(".sf-edit-btn").click(function(){
			var id = $(this).attr("data-id");
			window.location.href="#logistics/addtime/"+id;
		});
		$(".sf-del-btn").click(function(){
			var id = $(this).attr("data-id");
			var name = $(this).attr("temp_name");
			layer.confirm('确定删除吗？', {
				  btn: ['确定','取消']
			}, function(){
				common.loading();
				var param = {id: id, temp_name:name};
				common.common_ajax_request('/api/v1/store_freq_temp_del',param,false,function(data,code,msg){
					if(code==200 && msg.businessCode==0){
						initData2();
						layer.alert("删除成功!");
					}else{
						layer.alert("删除失败");
					}
				},"","post");
			}, function(){});
		});
	};
	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	var init = function() {
		initTpl();
		initData();
		initTab();
		
		initTpl2();
		initData2();
	};
	
	init();
});
